require 'test/unit'
require 'watir'

         #~ $un="crux@eforceglobal.com"
         #~ $pwd="wafer"
         #~ $ip="10.7.10.178"
         #~ $ie = Watir::IE.start('http://'+$ip+'/cruxWebUI/CruxHome.aspx')
         #~ $filepath="E:/Crux_Automation"

    class TC_02Users < Test::Unit::TestCase

 
  #~ def test_01Login

#~ require 'watir'
 
#~ puts "User is going to login to the Crux Application"
#~ sleep 5
#~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtUserName$txtWaterMarkedTextBox").set $un
#~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtPassword$txtWaterMarkedTextBox").set $pwd
#~ $ie.button(:name,"ctl00$ContentPlaceHolder1$btnLogin").click
#~ puts "Successfully logged into the Application"
#~ sleep 10

 #~ end
 
 
  def test_02AddUser
  
require 'xls'
require 'watir'

xlFile = XLS.new($filepath+ "/Crux_Data/Add_User.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Add User Functionality"
puts "**************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
  sleep 5
  
  puts "Verifying existing Company : '#{record['CompanyName']}' for creating User"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
    a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageUsers").click    
    sleep 8
    $ie.button(:name,"ctl00$ContentPlaceHolder1$btnAddNewUser").click
    sleep 5
    puts "Creating User :'#{record['UserName']}'"
    
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtUserName_txtWaterMarkedTextBox').set(record['UserName'])
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtFirstName_txtWaterMarkedTextBox').set(record['FirstName'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtLastName_txtWaterMarkedTextBox').set(record['LastName']) 
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtUserComments_txtWaterMarkedTextBox').set(record['Comments']) 
    $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
    sleep 5   
    puts "User: '#{record['UserName']}'created successfully"
 
    puts "Verifying the created User: '#{record['UserName']}'"
 
     c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
   
     #puts c
     
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][2].text 
    #puts u    
    if u=="#{record['UserName']}"      
      puts "User :'#{record['UserName']}' verified successfully"
      break
    end
    end  
      break  
  end   
end
end
 
 end
 
 
    def test_03EditUser

require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Edit_User.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Edit User Functionality"
puts "**************************"

myData.each do |record|
  
    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Companies").click
    sleep 5
  
    puts "Verifying existing Company : '#{record['CompanyName']}' for User details"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageUsers").click    
    sleep 8    
    puts "Verifying User: '#{record['UserName']}'for update"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][2].text 
    #puts u    
    if u=="#{record['UserName']}"      
      puts "User :'#{record['UserName']}' verified successfully"
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][6].button(:src,"http://"+$ip+"/CruxWebUI/appimages/edit_user.gif").click  
      sleep 5
      puts "Updating the User: '#{record['UserName']}' as '#{record['EUserName']}'"
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtUserName_txtWaterMarkedTextBox').set(record['EUserName'])
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtFirstName_txtWaterMarkedTextBox').set(record['EFirstName'])
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtLastName_txtWaterMarkedTextBox').set(record['ELastName']) 
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtUserComments_txtWaterMarkedTextBox').set(record['EComments']) 
      $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
      sleep 5   
      puts "User: '#{record['UserName']}' successfully updated as '#{record['EUserName']}'"
 
      puts "Verify the updated User: '#{record['EUserName']}'"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
   
     #puts c
     
     for k in 2..rc do
   eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[k][2].text 
    #puts eu  
    if eu=="#{record['EUserName']}"      
      puts "Updated User :'#{record['EUserName']}' verified successfully"
      break
    end
    end
      
      break
    end
    end
    break
    end  
  end
  end

end 

    def test_04DeleteUser

require 'xls'
require 'watir'
require 'watir/ie' 
require 'watir/contrib/enabled_popup'

xlFile = XLS.new($filepath + "/Crux_Data/Delete_User.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Delete User Functionality"
puts "**************************"

myData.each do |record| 

    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Companies").click
    sleep 5
  
    puts "Verifying a Company : '#{record['CompanyName']}' for User"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}" 
      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageUsers").click    
    sleep 8    
    puts "Verifying User : '#{record['UserName']}'for Delete"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][2].text 
    #puts u   
    
    if u=="#{record['UserName']}"      
      puts "User :'#{record['UserName']}' verified successfully"    
      
      puts "Deleting the User: '#{record['UserName']}'"
      
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][6].button(:src,"http://"+$ip+"/CruxWebUI/appImages/delete.gif").click_no_wait
      sleep 5
      
      #--------------------------------------------------------------------------------------------
      sleep 2  
       hwnd = $ie.enabled_popup(5)

       if (hwnd)  #yeah! a popup
       popup = WinClicker.new
       popup.makeWindowActive(hwnd)
       popup.clickWindowsButton("Microsoft Internet Explorer", "OK", 30)
       end
 
    sleep 8    
  
      #-----------------------------------------------------------------------------------------------
  
      puts "User: '#{record['UserName']}' deleted successfully "
 
      puts "Verifying the Deleted User: '#{record['UserName']}'"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
   
     #puts c
     
     for k in 2..rc do
   eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[k][2].text 
    #puts eu  
    if eu=="#{record['UserName']}"   
      flag=1      
      break
    end
  end
  
  if flag==1
    puts "User '#{record['UserName']}' not deleted successfully"
    else
      puts "Deleted User '#{record['UserName']}' verified successfully"
      end
      
      break
    end
    end
    break
    end  
  end
  
  end

end

def test_05ManageUser
 
require 'xls'
require 'watir'

xlFile = XLS.new(Dir.pwd + '/Crux_Data/Manage_Users.xls') #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Manage User Functionality"
puts "**************************"

myData.each do |record|
  
    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Companies").click
    sleep 5
  
    puts "Verifying existing Company : '#{record['CompanyName']}' for User details"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageUsers").click    
    sleep 8    
    puts "Verifying User: '#{record['UserName']}'for update"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][2].text 
    #puts u    
    if u=="#{record['UserName']}"      
      puts "User :'#{record['UserName']}' verified successfully"
      puts "Updating Manage user details for User: '#{record['UserName']}'" 
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][6].button(:src,"http://"+$ip+"/CruxWebUI/appimages/edit_user_details.gif").click  
      sleep 5
      
      rol="#{record['Roles']}" 
      role=rol.split("/")
      role1=role.size
      
      for i in 0..role1-1    
      
      
      $ie.label(:text,role[i]).click   
      
      end 
      
      
      $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSaveChanges").click
      sleep 5
        puts "Manage user details for User: '#{record['UserName']}'updated successfully"       
      
      break
    end
    end
    break
    end  
  end
  end

end 

def test_06ResetPassword

require 'xls'
require 'watir'
require 'watir/ie' 
require 'watir/contrib/enabled_popup'

xlFile = XLS.new(Dir.pwd + '/Crux_Data/Reset_UserPassword.xls') #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "User Password Reset Functionality"
puts "**************************"

myData.each do |record| 

    $ie.link(:text,"Administration").click
    sleep 5

    $ie.link(:text,"Manage Companies").click
    sleep 5
  
    puts "Verifying a Company : '#{record['CompanyName']}' for User"   
 
    b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}" 
      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkManageUsers").click    
    sleep 8    
    puts "Verifying User : '#{record['UserName']}'for Delete"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][2].text 
    #puts u   
    
    if u=="#{record['UserName']}"      
      puts "User :'#{record['UserName']}' verified successfully"  
      puts "Resetting the password for the User :'#{record['UserName']}'"      
  
      
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdUsers")[j][6].button(:src,"http://"+$ip+"/CruxWebUI/appimages/reset_user_password.gif").click
      sleep 5
      
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtNewPassword_txtWaterMarkedTextBox').set(record['NewPassword'])      
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtConfirmedNewPassword_txtWaterMarkedTextBox').set(record['ConfirmPassword'])
      $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click     
      sleep 5
      puts "Reset Password for the User :'#{record['UserName']}'done successfully"   
      
      $ie.link(:id,"ctl00_Logout_lnkLogout").click
      sleep 5
      $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtUserName$txtWaterMarkedTextBox").set(record['UserName'])
      $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtPassword$txtWaterMarkedTextBox").set(record['NewPassword'])
      $ie.button(:name,"ctl00$ContentPlaceHolder1$btnLogin").click
      sleep 5 
      puts "Verifying User: '#{record['UserName']}' with updated password"
    
    if $ie.contains_text(record['Name'])
    puts "User: '#{record['Name']}' verified successfully with updated password"
    if $ie.contains_text(record['CompanyName'])
      puts "User Company: '#{record['CompanyName']}' verified successfully" 
      else
    puts "Error: could not find text: '#{record['CompanyName']}' not created successfully"
  end
end
      $ie.link(:id,"ctl00_Logout_lnkLogout").click
      sleep 5
      $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtUserName$txtWaterMarkedTextBox").set $un
      $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtPassword$txtWaterMarkedTextBox").set $pwd
      $ie.button(:name,"ctl00$ContentPlaceHolder1$btnLogin").click
      sleep 5
      
      break
    end
    end
    break
    end  
  end
  
end

puts "**************************"
puts "End of Users Functionality"
puts "**************************"

end


          #~ def test_05Logout
            #~ puts "Logging out from the Application"
            #~ $ie.link(:id,"ctl00_Logout_lnkLogout").click
            #~ puts "Successfully logged out from the Application"
            #~ puts "********************************"
            #~ puts "End of Users Functionality"
            #~ puts "********************************"
            #~ sleep 5
            #~ $ie.close
            
            #~ end
  

  end



